package com.lgw.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgw.demo.pojo.Idcard;
import com.lgw.demo.pojo.Person;
import com.lgw.demo.service.PersonService;

import java.util.List;

@Controller
public class PersonController {
    @Autowired
    PersonService personService;

    @RequestMapping("personlist")
    public String getAllUser(Model model,String name){
       if(name!=null ){
           model.addAttribute("list",personService.searchPersonByName(name));
       }else{
           model.addAttribute("list",personService.findAllPerson());
       }
        return "personlist.html";
    }

    @RequestMapping("personlistapp")
    @ResponseBody
    public List<Person> personlistapp(){
        return  personService.findAllPerson();
    }

    @RequestMapping("addperson")
    public String addperson(){
        return "addperson.html";
    }

    @RequestMapping("personcommit")
    public String personcommit(int age,String name,String code){
        Person person=new Person();
        person.setAge(age);
        person.setName(name);
        Idcard idcard=new Idcard();
        idcard.setCode(code);
        person.setIdcard(idcard);
        //1对1删除是多次对单表的删除
        int result=personService.addPerson(person);
        if(result>0){
            return "redirect:/personlist";
        }
        return "fail.html";
    }
    //一对多表的设计 一个用户可以拥有多个订单 每个订单只属于一个用户


}
